package com.dz.lm.web.controller.pay;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.constant.LmConstant;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.pay.WxPayService;
import com.dz.lm.web.controller.BasicControl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.io.IOException;
import java.util.Map;
import java.util.Vector;

/**
 * 微信支付
 * 
 * @author zhanghairong
 */
@Controller
@RequestMapping("/rewriteUrl/pay")
public class WxPayControl extends BasicControl{
	
	public Logger logger = LogManager.getLogger(WxPayControl.class);
    
	@Autowired
    private WxPayService wxPayService;
    
    /**
     * 统一下单
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/unifiedOrder")
    public Map<String, Object> unifiedOrder(HttpServletRequest request) throws ServiceException {
    	String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		} 
		Map<String, Object> retData = wxPayService.unifiedOrder(param);
		subjo.put("retData", retData);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 查询订单
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/orderquery")
    public Map<String, Object> orderquery(HttpServletRequest request) throws ServiceException {
    	String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : "";
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		Map<String, Object> retData = wxPayService.orderquery(param);
		subjo.put("retData", retData);
		return parameterEncrypt(request, subjo);
    }
    
    /**
     * 用户提现（商户支付金额至用户零钱账户）
     * 
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/withdraw")
    public Map<String, Object> withdraw(HttpServletRequest request) throws ServiceException {
    	String reqData = request.getAttribute("reqData") != null ? request.getAttribute("reqData").toString() : request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
		System.out.println("参数3========"+reqData);
		logger.info("longgger3========"+reqData);
    	JSONObject _reqData = JSONObject.parseObject(reqData);
    	JSONObject subjo = new JSONObject();
    	//参数校验
		Vector<String> inparam = new Vector<String>();
		//inparam.add("openid");
		for (String in : inparam) {
			if (!_reqData.containsKey(in)) {
				subjo = new JSONObject();
		    	subjo.put("result", "01");
		    	subjo.put("message", "参数" + in + "异常");
		    	return parameterEncrypt(request, subjo);
			}
		}
		Map<String, Object> param = JsonUtils.readValue(_reqData.toString());
		if (param.get("openid") == null || "".equals(param.get("openid"))) {
			subjo = new JSONObject();
	    	subjo.put("result", "02");
	    	subjo.put("message", "openid不能为空");
	    	return parameterEncrypt(request, subjo);
		}
		if (param.get("userId") == null || "".equals(param.get("userId"))) {
			param.put("userId", request.getAttribute("userId"));
		}
		int amount = 0;
		//amount = amount + 30;
		String desc = LmConstant.LM_PAY_BODY_wITHDRAWAL;
		//param.put("amount", amount);
		param.put("desc", desc);
		param.put("device_info", param.get("deviceId"));
		param.put("spbill_create_ip", param.get("ip"));
		
		Map<String, Object> retData = wxPayService.withdraw(param);
		subjo.put("retData", retData);
		if ("SUCCESS".equals(retData.get("result"))) {
			subjo.put("result", "00");
			subjo.put("message", "提现成功");
		} else {
			subjo.put("result", "01");
			subjo.put("message", "提现失败");
		}
		return parameterEncrypt(request, subjo);
    }
}
